<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JQuery easyui demo</title>

<style type="text/css">
#user_fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}

.fitem input {
	width: 160px;
}
</style>


<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.4.1/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.4.1/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.4.1/themes/color.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.4.1/demo/demo.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-easyui-1.4.1/${pageContext.request.contextPath}/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.4.1/${pageContext.request.contextPath}/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
	<table id="laws_dg" class="easyui-datagrid" style="height: 900px;"
		url="${pageContext.request.contextPath}/admin/laws/findAll.do" toolbar="#laws_toolbar" pagination="true"
		rownumbers="true" fitColumns="true" singleSelect="true"
		data-options="fit:false,border:false,pageSize:10,pageList:[10,20,30,40]">
		<thead>
			
			<!-- 管理表格-表头的修改 -->
			<tr>
				<th field="lawsNo" width="50" height="20">法规编号</th>
				<th field="title" width="50">法规名称</th>
				<th field="sortNo" formatter="lawsFormat" width="50">法规类型</th>
				<th field="author" width="50">法规作者</th>
				<th field="content" width="50" >法规内容</th>
				<th field="posttime" width="50">发布时间</th>
				<th field="origin" width="50">法规来源</th>
			</tr>
		</thead>
	</table>
	
	<div id="laws_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newLaws()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editLaws()">编辑</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyLaws()">删除</a>
	</div>

	<div id="laws_dlg" class="easyui-dialog" style="padding: 10px 20px"
		closed="true" buttons="#laws_dlg-buttons">
		<div class="ftitle">详细信息</div>
		<form id="laws_fm" method="post" novalidate>
			<div class="fitem">
				<label>法规标题:</label> <input name="title" class="easyui-textbox"
					required="true">
			</div>
			<div class="fitem">
				<label>法规类型:</label> <input id="id_lawsType" name="lawsType.sortNo" class="easyui-combobox"
					url="lawsType/findList.do" valueField="sortNo" textField="sortName" required="true"
					editable="false">
			</div>
			<div class="fitem">
				<label>法规作者:</label>  <input name="author" class="easyui-textbox"
					required="true">
			</div>
			<div class="fitem">
				<label>法规内容:</label> <input name="content" class="easyui-textbox" data-options="multiline:true" 
				value="This TextBox will allow the user to enter multiple lines of text." style="width:600px;height:300px"
				required="true">
			</div>
			<div class="fitem">
				<label>发布时间:</label> <input name="posttime" class="easyui-datebox">
			</div>
			<div class="fitem">
				<label>来源:</label> <input name="origin" class="easyui-textbox">
			</div>
		</form>
	</div>
	
	<div id="laws_dlg-buttons">
		<a href="javascript:void(0)" class="easyui-linkbutton c6"
			iconCls="icon-ok" onclick="saveLaws()" style="width: 90px">保存</a>
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#laws_dlg').dialog('close')"
			style="width: 90px">取消</a>
	</div>
	<script type="text/javascript">
	$.fn.datebox.defaults.formatter = function(date){
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		return y+'/'+m+'/'+d;
	}
	
	$.fn.datebox.defaults.parser = function(date){
		if(date){
			return  new Date(date)
		}else{
			return new Date();
		}
	}
	
	
		//使用格式化显示数据
		function lawsFormat(value, row, rowindex) {
			//用参数 row 来获取 lawsType对象,再调用里面的type属性
			return row.lawsType.sortName;
		}

	
		var url;
		function newLaws() {
			$('#laws_dlg').dialog('open').dialog('setTitle', '新增');
			$('#laws_fm').form('clear'); //清除表单数据
			url = '${pageContext.request.contextPath}/admin/laws/addLaws.do'; //按新增 调用的网址
			
			//从已经加载的datagride中所选择的行（row）中获取key值,修改下拉框的选中值  
		    
		    var lawsTypedata = $('#id_lawsType').combobox('getData');
		    if(lawsTypedata.length>0){
			  $('#id_lawsType').combobox('select',lawsTypedata[0].sortNo);
		    }; 		
		}
		
		function editLaws() {
			var row = $('#laws_dg').datagrid('getSelected');
			if (row) {
				$('#laws_dlg').dialog('open').dialog('setTitle', '编辑'); //打开对话框
				$('#laws_fm').form('clear');   //清除表单数据
				$('#laws_fm').form('load', row);  //重新加载数据
				url = '${pageContext.request.contextPath}/admin/laws/updateLaws.do?lawsNo=' + row.lawsNo;
				
				//从已经加载的datagride中所选择的行（row）中获取key值,修改下拉框的选中值  
			    
			    var lawsTypedata = $('#id_lawsType').combobox('getData');
			    if(lawsTypedata.length>0){
				  $('#id_lawsType').combobox('select',row.lawsType.sortNo);
			    }; 
			}
		}
		function saveLaws() {
			$('#laws_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (result.errorMsg) {
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$('#laws_dlg').dialog('close'); // close the dialog
						$('#laws_dg').datagrid('reload'); // reload the user data
					}
				}
			});
		}
		function destroyLaws() {
			var row = $('#laws_dg').datagrid('getSelected');
			if (row) {
				$.messager.confirm('Confirm', '确定要删除吗?', function(r) {
					if (r) {   //ajax调用-异步通信调用
						$.post('${pageContext.request.contextPath}/admin/laws/deleteLaws.do', {
							lawsNo : row.lawsNo
						}, function(result) {
							if (result.success) {
								$('#laws_dg').datagrid('reload'); // reload the user data
							} else {
								$.messager.show({ // show error message
									title : 'Error',
									msg : result.errorMsg
								});
							}
						}, 'json');  //回调函数
					}
				});
			}
		}
	</script>

</body>
</html>